<div id="tab-wrapper">
	<h3>Custom WebServices</h3>
	<hr>
	<div id="custom-webservices-div">There are no custom webservices
		added.Please add them by clicking the Add a WebService button.</div>
	<br />

	<div id="custom-webservices-div-edit"></div>



	<div id="">
		<fieldset>
			<legend>Add a Web Service</legend>
			<form id="custom-web-service-form">
				<ul>
					<li><label for="webservice-name">Name :</label><input size=40
						type="text" id="webservice-name" name="webservice-name" /></li>
					<li><label for="webservice-wsld">WSDL :</label><input size=70
						type="text" id="webservice-wsdl" name="webservice-wsdl" /></li>
					<li><label for="webservice-operation">Operation :</label><input
						size=40 type="text" id="webservice-operation"
						name="webservice-operation" /></li>

					<li><label for="webservice-operation">&nbsp;</label>
						<button id="add-webservice-btn">Add a WebService</button></li>
				</ul>
			</form>

		</fieldset>
	</div>

	<p>Note: This is a work in progress, custom webservices can be used
		after a workflow recommendation engine is introduced.</p>

</div>
<script type="text/javascript">
//Load custom WebServices
function loadcustomwebservices() {

	$
			.getJSON(
					'index.php/tabcontroller/getcustomwebservices',
					function(data) {

						if (data.length > 0) {

							var i = 0;
							var text = "<table><tr><th>S No.</th><th>Name</th><th>WSDL</th><th>Operartion</th></tr>";

							$
									.each(
											data,
											function(key, val) {
												text = text
														+ "<tr><td>"
														+ i
														+ "</th><td>"
														+ val.descr
														+ "</td><td><a href="
														+ val.wsdl
														+ ">"
														+ val.wsdl
														+ "</a></td><td>"
														+ val.method
														+ "</td>"
														+ "<td><button serviceid="
														+ val.serviceid
														+ " descr='"
														+ val.descr
														+ "' wsdl='"
														+ val.wsdl
														+ "' method='"
														+ val.method
														+ "' class='custom-web-edit-btn'>Edit</button>"
														+ "<td><button serviceid="
														+ val.serviceid
														+ " class='custom-web-del-btn'>Delete</button>"
														+ "</tr>";
												i++;
											});
							text = text + "</table>";

							$('#custom-webservices-div').html(text);

							$('.custom-web-edit-btn')
									.each(
											function() {
												$(this)
														.click(
																function() {
																	console
																			.log("Adding");
																	var descr = $(
																			this)
																			.attr(
																					'descr');
																	var serviceid = $(
																			this)
																			.attr(
																					'serviceid');
																	var wsdl = $(
																			this)
																			.attr(
																					'wsdl');
																	var method = $(
																			this)
																			.attr(
																					'method');
																	var intext = '<fieldset>'
																			+ '<table><tr><th>Name</th><th>WSDL</th><th>Method Name</th></tr>'
																			+ '<tr><td><input type="text" id="custom-web-name" name="custom-web-name" value="'
																			+ descr
																			+ '" /></td>'
																			+ '<td><input type="text" id="custom-web-wsdl" value="'
																			+ wsdl
																			+ '"  /></td>'
																			+ '<td><input type="text" id="custom-web-method" value="'
																			+ method
																			+ '" /></td></tr></table>';
																	intext = intext
																			+ '<button id="custom-webservice-edit-save">Save Edit</button></fieldset>';
																	$(
																			'#custom-webservices-div-edit')
																			.html(
																					intext);

																	$(
																			'#custom-webservice-edit-save')
																			.button()
																			.click(
																					function() {
																						var editstring = '&serviceid='
																								+ serviceid
																								+ '&descr='
																								+ $(
																										'#custom-web-name')
																										.val()
																								+ '&wsdl='
																								+ $(
																										'#custom-web-wsdl')
																										.val()
																								+ '&method='
																								+ $(
																										'#custom-web-method')
																										.val();
																						console
																								.log(editstring);
																						$
																								.ajax({
																									type : "POST",
																									url : "index.php/tabcontroller/updatecustomwebservice",
																									data : editstring,
																									success : function(
																											data) {
																										alert(data);
																									}
																								});

																					});
																});
											});

						}
					});
}
$(document).ready(function(){


	//Load default webservices.
	loadcustomwebservices();

	//Add custom webservice
	$('#add-webservice-btn').click(function(event){
		event.preventDefault();
		add_custom_web_service($('#custom-web-service-form').serialize(),'custom-web-service-form');
		loadcustomwebservices();
	});
});


</script>
